<?

#	This file is part of SmartWebIF.
#	
#	SmartWebIF is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#	
#	SmartWebIF is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with SmartWebIF.  If not, see <http://www.gnu.org/licenses/>.

$config = include 'config.php';
include 'functions.php';

if (($config["password"] <> '') and ($_COOKIE["auth"] <> $config["password"]) or ($_COOKIE["time"] <> '' and $_COOKIE["time"] < time())) die();

$sdvb = get_sdvb_info($config);
if ($sdvb["data_path_err"]) die();

$service = get_service_info($config);

echo '
<!doctype html>
<html lang="en">
<head>
<script>
  $(function() {$( ".b-refresh" ).button({icons:{primary:"ui-icon-refresh"}});});
  $(function() {$(".b-record").button({icons:{primary:"ui-icon-bullet"}});});
  $(function() {
    $( "#accordion-epg" ).accordion({
	  collapsible: true,
	  heightStyle: "content"
	  });
	});
</script>
</head>
';

echo '<body>';
echo '<table width="100%"><tr>';
echo '<td><div class="service">'.$service["servicename"].'</div><b>'.$service["serviceprovidername"].'</b><br></td>';
echo '<td align="right" valign="top"><form><button type="submit" name="tab" value="3" class="b-refresh">Refresh EPG</button></form></td>';
echo '</tr></table>';

	$db = new SQLite3($config["data_path"].'\smarteit.db');
	$sql = 'select datetime from eit where tid = '.$config["tid"].' and sid = '.$config["sid"].' and osatid = '.$config["satid"].' and datetime <= '.time().' order by datetime desc limit 2';	
	$nowdate = $db->querySingle($sql) or die('<br>No EPG records found.');
	if ($nowdate)
		{
		$sql = 'select * from eit where tid = '.$config["tid"].' and sid = '.$config["sid"].' and osatid = '.$config["satid"].' and datetime >= '.$nowdate.' order by datetime limit 20';	
		$rs = $db->query($sql) or die('<br>Current EPG event not found.');

		echo '<div id="accordion-epg">';
		$count = 1;
		while($row = $rs->fetchArray(SQLITE3_ASSOC))
			{
			$datetime = date('d/m/Y H:i', $row["datetime"]);
			$enddatetime = date('H:i', $row["enddatetime"]);
			$mins = date('i', ($row["enddatetime"] - strtotime('now')));
			
#			foreach(mb_list_encodings() as $chr){echo mb_convert_encoding($row["name"], 'UTF-8', $chr)." : ".$chr."<br><br>";}
			$row["name"] = mb_convert_encoding($row["name"], "UTF-8", "ANSI, ISO-8859-2, ISO-8859-1");
			$row["shortdesc"] = mb_convert_encoding($row["shortdesc"], "UTF-8", "ANSI, ISO-8859-2, ISO-8859-1");
			$row["extdesc"] = mb_convert_encoding($row["extdesc"], "UTF-8", "ANSI, ISO-8859-2, ISO-8859-1");
			if ($count == 1 and (($row[datetime] > time()) or ($row[enddatetime] < time))) exit;
			echo '<h3>';
			echo '<b>'.$datetime.' - '.$enddatetime.' | '.$row["name"].'</b><br>';
			if ($row["shortdesc"] <> '') echo $row["shortdesc"];
			echo '</h3>';
			echo '<div>';
			if ($count == 1) echo '<form method="get" action="index.php"><input type="hidden" name="control" value="start_record"><input type="hidden" name="rectime" value="'.$mins.'"><button type="submit" class="b-record">Record current event</button></form><br>';
			else echo '<form method="get" action="index.php"><input type="hidden" name="control" value="schedule"><input type="hidden" name="event" value="'.$row[name].'"><input type="hidden" name="datetime" value="'.$row[datetime].'"><input type="hidden" name="enddatetime" value="'.$row[enddatetime].'"><input type="password" class="forms" name="schpass" size="10" placeholder="password">&nbsp;<button type="submit" class="b-record">Schedule event recording</button></form><br>';
			if ($row["shortdesc"] == $row["extdesc"]) $row["extdesc"] = '';
			if ($row["extdesc"] <> '') echo $row["extdesc"];
			echo '</div>';
			
			$count++;
			}

		echo '</div>';
		}

echo '</body>';
echo '</html>';

?>